package pkg

import (
	"os"
	"path/filepath"
)

func CreateFileSafely(filePath string) (*os.File, error) {
	// Extract the directory part of the file path.
	dir := filepath.Dir(filePath)

	// Create the directory if it does not exist.
	if err := os.MkdirAll(dir, os.ModePerm); err != nil {
		return nil, err
	}

	// Open or create the file with os.Create or os.OpenFile.
	file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		return nil, err
	}

	return file, nil
}
